<?php
exit;
include_once($_SERVER["DOCUMENT_ROOT"]. "/core/inc.php");
include_once($_SERVER["DOCUMENT_ROOT"]. "/include/user/inc.php");
include_once($_SERVER["DOCUMENT_ROOT"]. "/include/mblog/inc.php");
include_once($_SERVER["DOCUMENT_ROOT"]. "/include/album/inc.php");
$g_cgival = array();
$g_show = array();
$g_pro = array();

if(! security_check::check_refer())
{
	throw new Exception("来路不明");
}

function check_cgi_pro()
{
	global $g_cgival, $g_show, $g_pro;
	cgi::both($g_cgival['t'],"t","json");
	$g_cgival['t'] = string::un_html($g_cgival['t']);

	cgi::both($g_cgival['callback'], "callback", "");
	$g_cgival['callback'] = string::un_html(string::un_script_code($g_cgival['callback']));
	
	
	cgi::both($g_cgival['album_id'],"album_id",0);
	cgi::both($g_cgival['topic_id'],"topic_id",0);
	
	cgi::both($g_cgival['pic'],"pic","");
	$g_cgival['pic'] = string::un_html($g_cgival['pic']);
	
	cgi::both($g_cgival['content'],"content","");
	$g_cgival['content'] = string::un_html($g_cgival['content']);
}


function get_data()
{
	global $g_cgival, $g_show, $g_pro;
	
	$pic = $g_cgival['pic'];
	$content = $g_cgival['content'];
	$pid = $g_cgival['pid'];
	$album_id = $g_cgival['album_id'];
	$user_info = new user_info();
	$topic_id = $g_cgival['topic_id'];
	$is_login = $user_info->is_logined();
	$db_mblog_cron_topic_tag = new db_mblog_cron_topic_tag();
	if($is_login)
	{
		$user_detail_info = $user_info->get_cur_user_info();
		
		$uid = $user_detail_info["uid"];
		
	}
	else
	{
		throw new Exception("未登录！");
	}
	
	if(!$pic)
	{
		throw new Exception("图片呢？！");
	}
	
	
	preg_match_all("/\.sinaimg\.cn\/(.*?)\//",$pic,$match);
	
	if($match[1][0])
	{
		$is_sina_pic = true;
	}
	$pic = str_replace($match[1][0],"bmiddle",$pic);

	$header_info = @get_headers($pic);

	if(stripos($header_info[0],"200") == false)
	{
		throw new Exception("图片格式有误");
	}

	
	
	$db_mblog_pic_content = new db_mblog_pic_content();
	if(!$content)$content="图片分享";
	
	if($is_sina_pic == true)//如果是sina图片直接入库
	{
		$res = get_image::get_image_size_by_url($pic);
		$srcW = $res["width"]; 
		$srcH = $res["height"]; 
		$pic_width_pubu = PUBU_WIDTH;
		$pic_height_pubu = intval($srcH/($srcW/$pic_width_pubu));
	}
	else
	{
		$pic_content = file_get_contents($pic);
		$res = get_image::get_image_size_local($pic_content);
		$srcW = $res["width"]; 
		$srcH = $res["height"]; 
		$pic_width_pubu = PUBU_WIDTH;
		$pic_height_pubu = intval($srcH/($srcW/$pic_width_pubu));
		
		
		$pic_content = base64_encode($pic_content);
		$mblog_from_api = new mblog_from_api();
		$res = $mblog_from_api->api_upload_pic($pic_content);
		$pic =$res["data"]["result"]["bmiddle_pic"] ;
			
		if(!$pic)
		{
			throw new Exception("图片推送过程中失败！估计是网络问题");
		}
	}
	
	
	if(!$topic_id)
	{
		$topic_id =0;
		$show_type = 4;
		$topic = "";
	}
	else
	{
		$topic_item = $db_mblog_cron_topic_tag->get_mblog_cron_topic_tag_item_by_id($topic_id);
		$topic = $topic_item["topic"];
		$show_type = $topic["show_type"];
	}
	
	$source ="游戏秀";
	$from = "游戏秀";
	
	$last_insert_id = $db_mblog_pic_content->insert_mblog_pic_content($content,$topic_id,$topic,$pic,$pic_width_pubu,$pic_height_pubu,$source,$show_type,$from,$user_detail_info["uid"],$user_detail_info["user_nickname"]);
	
	
	if(!$last_insert_id)
	{
		throw new Exception("您说的内容重复啦！");
	}
	else
	{
		
		$db_user_info = new db_user_info();
		$db_user_info->update_user_count_by_uid($uid,"","",1);
		
		
		if($album_id)
		{
			$db_album_info = new db_album_info();
			$item = $db_album_info->get_album_item($album_id);
			if($item["create_uid"] !=$uid )
			{
				throw new Exception("无权限！");
			}
			
			
			$db_album_to_mblog = new db_album_to_mblog();
			$status_count = $db_album_to_mblog->get_album_status_count_by_album_id($album_id);
			
			$mod_mblog = new mod_mblog();
			$res = $mod_mblog->add_mblog_to_album($item,$album_id,$last_insert_id,$status_count);
			
		
		}
		
		
		
		/*//更新feed
		$feed_data["create_time"] = time();
		$feed_data["content"] = $content;
		$feed_data["create_uid"] = $user_detail_info["uid"];
		$feed_data["create_nickname"] = $user_detail_info["user_nickname"];
		$feed_data["user_pic"] = $user_detail_info["user_pic"];
		$feed_data["pic"] = $pic;
		$feed_data["id"] = $res;
		
		$db_user_feed = new db_user_feed();
		$data_detail = serialize($feed_data);
		$db_user_feed->insert_user_feed($uid,$data_detail,2);*/
	
	}
}

 
try
{
	check_cgi_pro();
	get_data();

	$g_show["error"] = "0";
	$g_show["errmsg"] = "发布成功～页面手动刷新后生效";
}
catch (Exception $e)
{
	$g_show["error"] = "1";
	$g_show["errmsg"] = $e->getMessage();
}
show_pro();
exit();


function show_pro()
{
	global $g_cgival, $g_pro, $g_show, $config;
	
	if ($g_cgival['t'] == "serialize")
	{
		echo serialize($g_show);
	}
	elseif ($g_cgival['t'] == "json")
	{
		echo output::json("json", $g_show, $g_cgival['callback']);
	}
	elseif($g_cgival['t'] == "jsonp")
	{
		echo output::json("jsonp", $g_show, $g_cgival['callback']);
	}
	elseif($g_cgival['t'] == "perl")
	{
		$string = php_to_perl::php_array_to_perl_hash($g_show);
		$string = mb_convert_encoding($string, "GBK", "UTF-8");
		echo $string;
	}
	elseif($g_cgival['t'] == "iframe")
	{
		header("location:{$g_cgival['link']}");
	}
	else
	{
		
		echo output::json("jsonp", $g_show, $g_cgival['callback']);
	}
}